package page;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import ressource.CadreProduit;
import ressource.CadreSouhait;
import ressource.CadreVendeur;

/**
 * Servlet implementation class VueSouhaitServlet
 */
public class VueSouhaitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VueSouhaitServlet() {
        super();
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dis;
		Connection con = null;
		
		String sid = request.getParameter("produit");
		Integer id = 0;
		CadreProduit p;
		Map<CadreVendeur, CadreSouhait> souhaits = new LinkedHashMap<CadreVendeur, CadreSouhait>();
		CadreVendeur v;
		CadreSouhait s;
		
		if(sid != null){
			try{
				id = Integer.parseInt(sid);
			}catch(Exception e){
				dis=request.getRequestDispatcher("home");
				dis.forward(request,response);
			}
			
			try{
				Context namingContext = new InitialContext();
				DataSource dataSource = (DataSource)namingContext.lookup("java:comp/env/jdbc/MiniIBay");
				con = dataSource.getConnection();
				
				//Recuperation des informations du produit
				PreparedStatement pstmt=con.prepareStatement("SELECT `p`.id_produit, `tp`.nom, `p`.intitule, `p`.description, `p`.photo FROM `produit` p, `type_produit` tp WHERE `p`.id_type = `tp`.id_type AND `p`.id_produit = ?");
				pstmt.setInt(1, id);
				ResultSet rs = pstmt.executeQuery();
				
				if(rs.next()) {
					p = new CadreProduit();
					p.setId(rs.getInt("id_produit"));
					p.setType(rs.getString("nom"));
					p.setIntitule(rs.getString("intitule"));
					p.setDescription(rs.getString("description"));
					p.setPhoto(rs.getString("photo"));
					
					request.setAttribute("produit", p);
				}
				
				//Recuperation des souhaits sur le produit
				pstmt=con.prepareStatement("SELECT `m`.id_membre, `m`.pseudo, `s`.prix, `s`.date, `s`.commentaire FROM `souhait` s, `membre` m WHERE `s`.id_membre=`m`.id_membre AND `s`.id_produit=? ORDER BY `s`.prix");
				pstmt.setInt(1, id);
				rs = pstmt.executeQuery();
				
				while(rs.next()) {
					v= new CadreVendeur();
					v.setIdVendeur(rs.getInt("id_membre"));
					v.setPseudo(rs.getString("pseudo"));
					
					s = new CadreSouhait();
					s.setPrix(rs.getFloat("prix"));
					s.setDate(rs.getTimestamp("date"));
					s.setCommentaire(rs.getString("commentaire"));
					
					souhaits.put(v, s);
				}
				
				request.setAttribute("souhaits", souhaits);
				
				dis=request.getRequestDispatcher("souhait.jsp");
				dis.forward(request,response);
			}catch(Exception e){
				e.printStackTrace();
				dis=request.getRequestDispatcher("home");
				dis.forward(request,response);
			} finally {
				try {
					if(con != null) con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
